/**
 * Created by Administrator on 2015/2/5 0005.
 */
var phoneApp = angular.module("app.phoneIndex", []);
var interviewId,interview,userType = '/recruitStudent/privatePhone',userName;
function getUserRoleName(){var roleName = $("#userRoleName").val();if(roleName == 'phoneAdmin' || roleName == 'admin' ||roleName == 'deptMain') return true;else return false;}
function del($http,obj,id){
    $http.get('management/interview/del?id='+id).success(function(data){
        var copyObj = obj;
        for(var i =0 ;i<copyObj.length;i++){
            if(copyObj[i].id == id){obj.splice(i,1);}
        }
    });
}
function openPhoneInfo(ngDialog){
    ngDialog.open({
        template: 'phoneTrace/phoneInfo',
        className:'ngdialog-theme-flat',
        width:680, height:430
    });
}
function openUpdatePhone(ngDialog){
    ngDialog.open({
        template: 'phoneTrace/updatePhone',
        className:'ngdialog-theme-flat', width:780, height:560
    });
}
function getSelectLabel(id){return $("#"+id).find("option:selected").text();}
function getData(url,$http,$scope){
    $http.get(url).
        success(function(data) {
            $scope.phones =  data.list;
            $scope.total = data.total;
            $scope.pageSize = data.pageSize;
            $scope.numPerPage = 1;
            $scope.search = "";
            $scope.phones.length == 0 ? $scope.phoneAddHide = false : $scope.phoneAddHide = true;
        });
}
//自定义的一个过滤器
phoneApp.filter("dataFormat",function(){return function(input){return input.toString().substring(0,input.toString().lastIndexOf(":"));}});
phoneApp.filter("distill",function(){return function(input){return input == '1'?true:false;}});
phoneApp.filter("abate",function(){return function(input){input = input.toString().substring(0,input.toString().lastIndexOf(" "));var addTime = new Date(input);var nowTime = new Date();var timeCha = (nowTime.getTime()-addTime.getTime());if(timeCha >= (1000*60*60*24*7)){return false;}else{return true;}}});
//全局搜索
phoneApp.controller("phoneIndex",['$scope', '$http','$filter','ngDialog','$location',function($scope, $http,$filter,ngDialog,$location) {
    $scope.phoneAddHide = true;
    $scope.keyUpGetData = function(){
        if($scope.search.length == 0){$scope.phones = {};$scope.total = 0;$scope.pageSize=0;return;}
        var name = $scope.search;var reg = /^[\u4e00-\u9fa5]*$/;var reg1 = /^[0-9]{1,11}$/;
        if(reg.test(name)&&name.length<6){$scope.pageChange(1,false);
        }else if(reg1.test(name)&&name.length>3){$scope.pageChange(1,false);}
    };$scope.queryDate = false;var endDate,startDate;
    $scope.pageChange = function(page,bool){
        if(bool != null)$scope.queryDate = bool;
        if($scope.queryDate){endDate = $("#end").val();startDate = $("#start").val();}
        else{endDate = null;startDate = null;}
        $http.get('management/interview/findByPage',{params: {pageNum:page,search:$scope.search,startDate:startDate,endDate:endDate}}).
            success(function(data) {
                $scope.phones =  data.list;$scope.total = data.total;$scope.pageSize = data.pageSize;$scope.numPerPage = 1;
                $scope.phones.length == 0 ? $scope.phoneAddHide = false : $scope.phoneAddHide = true;
            });
    };
    $scope.order = function(rowName) {
        return $scope.row !== rowName ? ($scope.row = rowName,
            $scope.phones = $filter("orderBy")($scope.phones, rowName), $scope.onOrderChange()) : void 0
    };
    $scope.clickToOpen = function (id,name) {
        interviewId = id;userName = name;
        ngDialog.open({template: 'phoneTrace/trackInfo', className:'ngdialog-theme-flat', width:980, height:600});
    };
    $scope.delInterview = function(id){del($http,$scope.phones,id);};
    $scope.phoneInfo = function(id){interviewId = id;openPhoneInfo(ngDialog);};
    $scope.updatePhone = function(id,obj){interviewId = id;interview = obj;openUpdatePhone(ngDialog);};
    $scope.distill = function(id){
        $http.get('management/interview/distill?id='+id).success(function(data){
            $location.path(userType);
        });
    }
}]);
//个人搜索
function getkeyup($scope){
var name = $scope.search;var reg = /^[\u4e00-\u9fa5]*$/;var reg1 = /^[0-9]{1,11}$/;
if(reg.test(name)&&name.length<6){$scope.getChange(1);
}else if(reg1.test(name)&&name.length>3){$scope.getChange(1);}}
phoneApp.controller("privatePhone",['$scope','$http','ngDialog','$filter',function($scope,$http,ngDialog,$filter){
    getData('management/interview/findByUserId?pageNum=1',$http,$scope);$scope.userId;$scope.sitId;
    $scope.keyupQuery = function(){getkeyup($scope);};$scope.queryDate = false;var endDate,startDate;
    $scope.pageChange = function(page,bool){if(bool != null)$scope.queryDate = bool;
        if($scope.queryDate){endDate = $("#end").val();startDate = $("#start").val()==null?"":$("#start").val();}
        $http.get('management/interview/findByUserId',{params: {pageNum:page,search:$scope.search,
            startDate:startDate,endDate:endDate,userId:$scope.userId,sitId:$scope.sitId}}).
            success(function(data) {
                $scope.phones =  data.list;$scope.total = data.total;
                $scope.pageSize = data.pageSize;$scope.numPerPage = 1;
                $scope.phones.length == 0 ? $scope.phoneAddHide = false : $scope.phoneAddHide = true;
            });
    };
    $scope.getChange = function(page){
        var startDate = $scope.search.length == 0?$("#start").val():"";
        $http.get('management/interview/findByUserId',{params: {pageNum:page,search:$scope.search,startDate:startDate,
            endDate:endDate,userId:$scope.userId,sitId:$scope.sitId}}).
            success(function(data) {
                $scope.phones =  data.list;$scope.total = data.total;
                $scope.pageSize = data.pageSize;$scope.numPerPage = 1;
                $scope.phones.length == 0 ? $scope.phoneAddHide = false : $scope.phoneAddHide = true;
            });
    };
    $scope.userPerHide = getUserRoleName();
    if($scope.userPerHide){
        $http.get('management/user/findUserByRoleAndJobState').success(function(data){
            $scope.user = data;
        });
        $http.get('management/situation/findByStateY').success(function(data){
            $scope.situation = data;
        });
    }
    $scope.order = function(rowName) {
        return $scope.row !== rowName ? ($scope.row = rowName,
            $scope.phones = $filter("orderBy")($scope.phones, rowName), $scope.onOrderChange()) : void 0
    };
    $scope.clickToOpen = function(id,name){
        interviewId = id;userName = name;
        ngDialog.open({template: 'phoneTrace/trackInfo', className:'ngdialog-theme-flat',
            width:980, height:600});
    };
    $scope.delInterview = function(id){del($http,$scope.phones,id);};
    $scope.phoneInfo = function(id){interviewId = id;openPhoneInfo(ngDialog);};
    $scope.updatePhone = function(id,obj){interviewId = id;interview = obj;openUpdatePhone(ngDialog);}
}]);
//追踪详情
phoneApp.controller("trackInfo",['$scope','$http','$timeout',function($scope,$http,$timeout){
    //AJAX传参方式 {params: {id:'5'}
    $scope.isHide = true;
    $http.get('management/situation/findByStateY').success(function(data){
        $scope.situation = data;
    });
    $http.get('management/interviewerTrace/findByIntervId?id='+interviewId).success(function(data){
        $scope.trackInfo = data;
    });
    $scope.form = {};
    $scope.form.situId = '1';
    $scope.addTrack = function(){
        $scope.form.intervId = interviewId;
        $scope.form.userName = userName;
        $http({
            method  : 'post',
            url     : 'management/interviewerTrace/add',
            data    : $.param($scope.form),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            $http.get('management/interviewerTrace/findByIntervId?id='+interviewId).
                success(function(data){$scope.trackInfo = data;});
            $scope.form = {};
        }).error(function(data){
            $scope.isHide = false;
        });
        $timeout(function(){$scope.isHide = true;},5000);
    }
}]);
//添加面试者信息
phoneApp.controller('addPhone',['$scope','$http','$location','$timeout',function($scope,$http,$location,$timeout){
    $scope.isHide = true;$scope.phoneForm ={};
    $scope.phoneForm.eduId = "1";$scope.phoneForm.sourceId = "1";$scope.phoneForm.sitName = "1";
    $http.get('management/situation/findByStateY').
        success(function(data){
            $scope.situation = data;
        });
    $http.get('management/education/findByStateY').success(function(data){
        $scope.edu = data;
    }),
        $http.get('management/enroll/findByStateY').success(function(data){
            $scope.enroll = data;
        }),
        $scope.addInterview = function(){
            $scope.isHide = false;
            $http({
                method  : 'post',
                url     : 'management/interview/add',
                data    : $.param($scope.phoneForm),
                headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function(data){
                if(data > 0){
                    $scope.showInfo = '系 统 检 测 到 该 条 数 据 已 有';
                }else{$scope.showInfo = '';$location.path(userType);}
            }).error(function(data){
                $scope.showInfo = '添 加 失 败'
            });
            $timeout(function(){$scope.isHide = true;},5000);
        }
}]);
//面试者详细
phoneApp.controller("phoneInfo",['$scope','$http','ngDialog',function($scope,$http){
    $http.get("management/interview/findInfo?id="+interviewId).success(function(data){
        $scope.info = data;
    });
}]);
//修改面试者信息
phoneApp.controller("updatePhone",['$scope','$http','ngDialog','$timeout','$location',function($scope,$http,ngDialog,$timeout){
    $http.get("management/interview/findById?id="+interviewId).success(function(data){
        $scope.info = data;
    });
    $http.get('management/education/findByStateY').success(function(data){
        $scope.edu = data;
    });
    $http.get('management/enroll/findByStateY').success(function(data){
        $scope.enroll = data;
    });
    $scope.userPerHide = getUserRoleName();
    if($scope.userPerHide){
        $http.get('management/user/findUserByRoleAndJobState').success(function(data){
            $scope.userItem = data;
        });
    }
    $scope.updateInterview = function(){
        $scope.isHide = false;
        $http({
            method  : 'post',
            url     : 'management/interview/update',
            data    : $.param($scope.info),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            $scope.showInfo = "修 改 成 功";
            interview.realName = $scope.info.realName;
            interview.phoneNum = $scope.info.phoneNum;
            interview.userName = getSelectLabel("user");
            interview.eduName = getSelectLabel("edu");
            interview.sourceName = getSelectLabel("source");
            $timeout(function(){
                ngDialog.close('phoneTrace/updatePhone');
            },1000);
        }).error(function(data){
            $scope.showInfo = "修 改 失 败";
        });
        $timeout(function(){$scope.isHide = true;},5000);
    }
}]);
//电话量统计
phoneApp.controller('phoneCount',['$scope','$http',function($scope,$http){
    $scope.countForm = {};
    $scope.countForm.countType = "0";
    $scope.isEndDateHide = true;
    $scope.countTypeChange = function(){
        $scope.countForm.countType == 2 ? $scope.isEndDateHide = false : $scope.isEndDateHide = true;
    };
    $scope.queryCount = function(){
        $http.get('management/interview/findByPage',
            {params: {type:$scope.countForm.countType,startDate:$("#startDate").val(),endDate:$("#endDate").val()}}).success(function(data){

            });
    };
    $scope.pieChart = {}, $scope.pieChart.data = [{
        label: "下载 Sales", data: 12
    }, {label: "哈哈 Sales", data: 30
    }, {label: "Mail-Order Sales", data: 20
    }, {label: "Online Sales", data: 19
    }], $scope.pieChart.options = {
        series: {pie: {show: !0}},
        legend: {show: !0},
        grid: {hoverable: !0, clickable: !0},
        colors: ["#23AE89", "#2EC1CC", "#FFB61C", "#E94B3B"],
        tooltip: !0,
        tooltipOpts: {content: "%p.0%, %s", defaultTheme: !1}
    }
}]);
//来源统计
phoneApp.controller('sourceCount',['$scope','$http',function($scope,$http){
    $scope.countForm = {};
    $scope.countForm.countType = "0";
    $scope.isEndDateHide = true;
    $scope.countTypeChange = function(){
        $scope.countForm.countType == 2 ? $scope.isEndDateHide = false : $scope.isEndDateHide = true;
    };
    $scope.pieChart = {},
        $scope.pieChart.data = [{
            label: "下载 Sales 12", data: 12
        }, {label: "哈哈 Sales", data: 30
        }, {label: "Mail-Order Sales", data: 20
        }, {label: "Online Sales", data: 19
        }], $scope.pieChart.options = {
            series: {pie: {show: !0}
            }, legend: {show: !0},
            grid: {hoverable: !0, clickable: !0},
            colors: ["#23AE89", "#2EC1CC", "#FFB61C", "#E94B3B"],
            tooltip: !0, tooltipOpts: {content: "%p.0%, %s", defaultTheme: !1}
        }
    $scope.test = function(){
        $scope.pieChart.data = [{
            label: "Sales 12", data: 34
        }, {label: "Sales", data: 56
        }, {label: "Mail-Order Sales", data: 80
        }, {label: "Online Sales", data: 56
        }], $scope.pieChart.options = {
            series: {pie: {show: !0}},
            legend: {show: !0},
            grid: {hoverable: !0, clickable: !0},
            colors: ["#23AE89", "#2EC1CC", "#FFB61C", "#E94B3B"],
            tooltip: !0, tooltipOpts: {content: "%p.0%, %s", defaultTheme: !1
            }
        };
    };
}]);
//Excel文件导出
phoneApp.controller('excelExport',['$scope','$http','ngDialog','$timeout',function($scope,$http){
    $scope.userPerHide = getUserRoleName();
    if($scope.userPerHide){
        $http.get('management/user/findUserByRole').success(function(data){
            $scope.userInfo = data;
        });
    };
    $http.get("management/interview/excelShowData",{params: {startDate:"true"}}).success(function(data){
        $scope.phones = data;
    });
    $scope.userId="";
    $scope.showDate = function(){
        if($("#errorHide").val().length > 0)return;
        $http.get("management/interview/excelShowData",{params: {startDate:$("#startDate").val(),
            endDate:$("#endDate").val(),userId:$scope.userId}}).success(function(data){
            $scope.phones = data;
        });
    };
}]);

//Excel文件上传
phoneApp.controller('uploadExcel', ['$scope', 'FileUploader','$location', function($scope, FileUploader,$location) {
    $scope.errorHide = true;
    var uploader = $scope.uploader = new FileUploader({
        url: 'management/interview/uploadExcel'
    });
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        if(response.length>0){
            $scope.errorHide = false;$scope.errorInfo = response;
        }else{
            $location.path(userType);
            $scope.errorHide = true;
        }
    };
}]);
//来源与情况
phoneApp.controller('souOrSit',['$scope','$http','$timeout',function($scope,$http,$timeout){
    $scope.sitForm = {};$scope.sitForm.state = 1;
    $scope.enrollForm = {};$scope.enrollForm.state = 1;
    $scope.enrollUpdate = false;$scope.situationUpdate = false;
    $http.get('management/enroll/findAll').success(function(data){
        $scope.enroll = data;
    });
    $http.get('management/situation/findAll').success(function(data){
        $scope.situation = data;
    });
    $scope.isHideEnroll = true;
    $scope.addEnroll = function(){
        $http({
            method  : 'post',
            url     : 'management/enroll/add',
            data    : $.param($scope.enrollForm),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            $scope.enrollForm = {};
            if(!$scope.enrollUpdate)
                $scope.enroll.push(data);
        }).error(function(data){
            $scope.isHideEnroll = false;
        });
        $timeout(function(){$scope.isHideEnroll = true;},5000);
    };
    $scope.isHideSitu = true;
    $scope.addSitu = function(){
        $http({
            method  : 'post',
            url     : 'management/situation/add',
            data    : $.param($scope.sitForm),
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function(data){
            $scope.sitForm = {};
            if(!$scope.situationUpdate)
                $scope.situation.push(data);
        }).error(function(data){
            $scope.isHideSitu = false;
        });
        $timeout(function(){$scope.isHideSitu = true;},5000);
    };
    $scope.updateEnroll = function(data){
        $scope.enrollUpdate = true;
        $scope.enrollForm = data;
    };
    $scope.updateSitu = function(data){
        $scope.situationUpdate = true;
        $scope.sitForm = data;
    };
    function delArray(obj,id){
        var copyObj = obj;for(var i =0 ;i<copyObj.length;i++){
            if(copyObj[i].id == id){obj.splice(i,1);}
        }
    };
    $scope.delEnroll = function(id){
        $http.get('management/enroll/del?id='+id).success(function(data){
            delArray($scope.enroll,id);
        });
    };
    $scope.delSitu = function(id){
        $http.get('management/situation/del?id='+id).success(function(data){
            delArray($scope.situation,id);
        });
    }
}]);